package com.zzu.model.impl;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.zzu.model.model.UserModel;
import com.zzu.model.bean.User;
import com.zzu.model.service.UserService;
import com.zzu.presenter.UserResponseListener;
import com.zzu.utils.ServiceCreator;
import com.zzu.utils.ToastAndDialogUtils;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-07 20:32
 * 和用户表相关的联网操作
 */
public class UserModelImpl implements UserModel {

    //获取UserService的接口实现类对象
    private UserService userService = ServiceCreator.getRetrofit(UserService.class);

    private final String TAG = UserModelImpl.class.getName();

//    Context context = null;
//    ProgressDialog dialog = null;
//    public UserModelImpl(Context context){
//        this.context = context;
//        //加载进度条
//        dialog = ToastAndDialogUtils.showDialog(context,"正在加载中");
//    }

    @Override
    public void login(String tel, String password, UserResponseListener<Boolean> userResponseListener) {

        Call<Boolean> call = userService.ifPermittedLogin(tel, password);

        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
//                ToastAndDialogUtils.cancelDialog(dialog);
                userResponseListener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
//                if(dialog != null){
//                    dialog.setMessage("请求服务器失败"+t);
//                }
                userResponseListener.onFailure(t);
                Log.i(TAG,"请求服务器失败"+t);
            }
        });

    }

    @Override
    public void register(User user, UserResponseListener userResponseListener) {
        Call<ResponseBody> call = userService.register(user.getId(), user.getPassword(), user.getName(), user.getRole(), user.getStatus(), user.getTel(),
                user.getTransNodeId());
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                ToastAndDialogUtils.cancelDialog(dialog);
                userResponseListener.onResponse(response);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
//                if(dialog != null){
//                    dialog.setMessage("请求服务器失败"+t);
//                }
                userResponseListener.onFailure(t);
                Log.i(TAG,"请求服务器失败\n"+t);
            }
        });

    }

    @Override
    public void checkIfUserExistByTel(String tel, UserResponseListener<Boolean> userResponseListener) {
        Call<Boolean> call = userService.checkIfUserExistByTel(tel);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                userResponseListener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                userResponseListener.onFailure(t);
                Log.i(TAG,"请求服务器失败\n"+t);
            }
        });
    }


}
